import { createRouter, createWebHashHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import localCache from '@/utils/localCache'
import { firstMenu } from '@/utils/mapRouteToMenu'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    redirect: '/main'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue')
  },
  {
    path: '/main',
    name: 'main',
    component: () => import('@/views/main/index.vue'),
    children: [
      {
        path: '/main/profile',
        component: () => import('@/views/main/Profile/Profile.vue')
      }
    ]
  },
  {
    path: '/:pathMatch(.*)',
    name: 'notfond',
    component: () => import('@/views/notfond/index.vue')
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to) => {
  document.title = (to.meta.title as string) ?? '后台管理系统'
  NProgress.start()
  if (to.path !== '/login') {
    const token = localCache.getCache('token', false)
    if (!token) {
      return '/login'
    }
  }
  if (to.path === '/main' && firstMenu) {
    return firstMenu.url
  }
  NProgress.done()
})

export default router
